// components/benefitCard/benefitCard.js
import {requestSubscribeMessage} from "../../utils/wxapi";
import {isShowReminderOfExpired} from "../../utils/publicMethods";
var netWork = require('../../utils/netWork.js');
let isClick = true;
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        pic_btn_bg:{
            type: String,
            value: ''
        },
        btn_color:{
            type: String,
            value: '#B72A23'
        },
        speed_img:{
            type: String,
            value: getApp().globalData.imagesUrl+'/common/red_speed.png'
        },
        text_color:{
            type: String,
            value: '#C71720'
        },
        speed_bg:{
            type: String,
            value: ''
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        show:false,
        data:"",
        status:1
    },

    /**
     * 组件的方法列表
     */
    methods: {
        click_btn(){
            if(this.data.status == 1){//待激活状态
                this.benefitCardDetails()
            }else{
                this.speedReceiveEntity()
            }
        },
        //权益卡跳转详情页
        async benefitCardDetails(e){
            let flag = await isShowReminderOfExpired(getApp().globalData.benefitCardSpeedData.prizeOrderId);
            if(flag){
                requestSubscribeMessage(getApp().globalData.benefitCardSpeedData.prizeOrderId);
            }
            wx.navigateTo({
                url: '/pages/commonPage/benefitCardDetails/benefitCardDetails?prizeOrderId='+getApp().globalData.benefitCardSpeedData.prizeOrderId
            })
        },
        speedReceiveEntity(){
            this.triggerEvent('speedReceiveEntity');
        },
        isShow(){
            let status = 1;
            let benefitCardSpeedData = getApp().globalData.benefitCardSpeedData;
            let doubleMoney = getApp().globalData.doubleMoney;
            if(benefitCardSpeedData){
                benefitCardSpeedData.median = parseInt((benefitCardSpeedData.totalTimes+1)/2)
                if(benefitCardSpeedData.totalTimes == benefitCardSpeedData.currentTimes){
                    status = 3;
                }
                if(benefitCardSpeedData.subjoinType == 1){//红包
                    if(benefitCardSpeedData.subjoinRecordId != doubleMoney.subjoinRecordId){
                        this.setData({
                            show:true,
                            data:benefitCardSpeedData,
                            status
                        })
                    }else{
                        this.setData({
                            show:true,
                            data:benefitCardSpeedData,
                            status:8
                        })
                    }
                }else{
                    this.setData({
                        show:true,
                        data:benefitCardSpeedData,
                        status
                    })
                }
            }
        },
        setReceivedSuccess(){
            this.setData({
                status:8
            })
        },
        updateOrder: function (event) {
            if (!isClick) {
                console.log("重复返回~");
                return;
            }
            if (isClick) {
                isClick = false;
                //定时器
                setTimeout(function () {
                    isClick = true;
                }, 3000); //3秒内不能重复点击
            }
            const that = this;
            wx.showModal({
                title: '提示',
                content: '确认收到货了吗？',
                success: function (sm) {
                    if (sm.confirm) {
                        //兑奖
                        netWork.POST('confirmCustomerSubjoinPrize', {
                            data: {
                                userId: wx.getStorageSync('userId'),
                                prizeOrderId: getApp().globalData.benefitCardSpeedData.prizeOrderId
                            },
                            success: function (res) {
                                if (res.data.retCode == undefined) {
                                    wx.showToast({
                                        title: '操作失败',
                                        icon: 'error'
                                    })
                                    return;
                                }
                                if (res.data.retCode == "0000") {
                                    wx.showToast({
                                        title: '操作成功',
                                        icon: 'success'
                                    })
                                    that.setData({
                                        status: 14
                                    })
                                } else {
                                    if(res.data.retCode == "10029"){
                                        let productType = wx.getStorageSync('productType');
                                        let error_url = productType == 4 ? '../../xinyouranPage/prompt/prompt' : productType == 1 ? '../../jingdianPage/prompt/prompt' : productType == 2 ? '../../prompt/prompt' : productType == 3 ? '../../ruiPage/prompt/prompt' : '../../xinyouranPage/xinyouranHighPage/prompt/prompt';
                                        wx.redirectTo({
                                            url: error_url + '?retCode=' + res.data.retCode + '&retMsg=' + res.data.retMsg,
                                        }) 
                                    }else{
                                        wx.showToast({
                                            title: '操作失败',
                                            icon: 'error'
                                        })
                                    }
                                }
                            },
                            fail: function () {
                                wx.showToast({
                                    title: '操作失败',
                                    icon: 'error'
                                })
                            }
                        });
                    } else if (sm.cancel) {
                        return;
                    }
                }
            })
        },
    }
})
